# Makefile

C_SOURCES = $(shell find . -name "*.c")
C_OBJECTS = $(patsubst %.c, %.o, $(C_SOURCES))
S_SOURCES = $(shell find . -name "*.s")
S_OBJECTS = $(patsubst %.s, %.o, $(S_SOURCES))

CC = gcc
LD = ld
ASM = nasm
OUT = mc_kernel
ISOOUT = myos.iso

C_FLAGS = -c -Wall -m32 -ggdb -gstabs+ -nostdinc -fno-pic -fno-builtin -fno-stack-protector -I include
LD_FLAGS = -T scripts/kernel.ld -m elf_i386 -nostdlib
ASM_FLAGS = -f elf -g -F stabs

build: $(S_OBJECTS) $(C_OBJECTS) link iso

.c.o:
	@echo 编译代码文件 $< ...
	$(CC) $(C_FLAGS) $< -o $@

.s.o:
	@echo 编译汇编文件 $< ...
	$(ASM) $(ASM_FLAGS) $<

link:
	@echo 链接内核文件...
	$(LD) $(LD_FLAGS) $(S_OBJECTS) $(C_OBJECTS) -o ${OUT}

.PHONY:iso
iso:
	cp ${OUT} iso/boot/${OUT}
	grub-mkrescue -o myos.iso iso
	
.PHONY:clean
clean:
	$(RM) $(S_OBJECTS) $(C_OBJECTS) ${OUT}

.PHONY:qemu
qemu:
	qemu-system-i386 -kernel ${OUT}

cdrom:
	qemu-system-i386 -cdrom ${ISOOUT}